<?php
namespace Pay\Controller;
use Think\Controller;
class IndexController extends Controller {
    //企业号corpid
    public $corpid=null;
    //企业号corpsecret
    public $corpsecret=null;
    /*测试数据*/
    private $info;
    
    public function __construct(){
        parent::__construct();
        $this -> corpid = C('CORPID');
        $this -> corpsecret  = C('SECRET');
        //echo createAuthUrl($this -> corpid,'Pay','Index','setpwd');die;

        /*测试数据*/
        $this->info = unserialize(file_get_contents('./Org/Chat/test.txt'));
    }
                    
    //获取 Access_token
    public function Access_token($corpid,$corpsecret){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret";
        $access_token = json_decode(file_get_contents($url),true)['access_token'];
        return $access_token;
    }

    //获取工资接口
    public function terminal($data){
        $secret = C('AWD_KEY');
        $access_key = C('AWD_SECRET');
        $cmd = 'hrms.getSalaries';
        $request_url = C('AWS_URL')."openapi";
        //当前时间戳 毫秒级
        $timestamp = round(microtime(true)*1000);
        //生成json数据
        $json = $data;
        //构造签名串
        $code = "{$secret}access_key{$access_key}cmd{$cmd}formatjsonsig_methodHmacMD5timestamp{$timestamp}zparams{$json}";
        //生成HMACMD5签名
        $sig = strtoupper(hash_hmac('md5',$code,$secret));
        //组织请求url
        $url = "{$request_url}?timestamp={$timestamp}&sig_method=HmacMD5&cmd={$cmd}&access_key={$access_key}&format=json&sig={$sig}&zparams={$json}";

        if(!get_headers($url,1)){
            return false;
            die;
        }
        $result = json_decode(file_get_contents($url),true);
        return $result;
    }
    
    //工资统计
    public function Statistics(){

        $start = I('post.start');
        $end = I('post.end');
        $start = strtotime($start);
        $end = strtotime($end);

        $data = [];
        foreach($this->info as $k=>$v){
            $a = strtotime($k);
            $data[$a] = $v;
        }
        $arr = [];
        foreach($data as $k=>$v){
            if($k>=$start && $k<=$end){
                $date = date('Y-m',$k);
                $arr[$date] = $v;
            }
        }
        //dump($arr);exit;
        $this->assign('data',$arr);
        $this->display();
        exit;
        $SY = date('Y',strtotime($start));
        $SM = date('m',strtotime($start));
        $EY = date('Y',strtotime($end));
        $EM = date('m',strtotime($end));
        $data = "{'YGBM':'".session('pay_bianhao')."','MONTH':'".$SM."','MONTH2':'".$EM."','YEAR':'".$SY."'}";
        $data = $this -> terminal($data);
        if(!$data){
            $this->display('Index/failget');
            die;
        }
        if(!session('pay_bianhao')){
            $this->redirect('/Pay/Index/fail');
        }
        if($data['errorCode'] === "400" && $data['result'] === "error"){
            $this->display('Index/fail');
            die;
        }
        $this -> assign('data',$data);
        $this -> display();
	}

    //单月工资
    public function info(){
        $time = I('post.time');
        $time = strtotime($time);
        $date = date('Y-m',$time);
        $uuid = session('pay_bianhao');
        $name = session('name');
        $data = $this->info[$date];
        //dump($data);exit;
        $this -> assign(array(
            'date' => $date,
            'name' => $name,
            'uuid' => $uuid,
            'data' => $data
        ));

        $this -> display();
        exit;
        $data = "{'YGBM':'".session('pay_bianhao')."','MONTH':'".$M."','MONTH2':'".$M."','YEAR':'".$Y."'}";
        /*测试数据开始*/
        $data = "{'YGBM':'5098018551','MONTH':'10','MONTH2':'10','YEAR':'2016'}";
        /*测试数据结束*/
        //dump($data);exit;
        $data = $this -> terminal($data);
        if(!$data){
            $this->display('Index/failget');
            die;
        }
        if(!session('pay_bianhao')){
            $this->redirect('/Pay/Index/fail');
        }

       

        if($data['errorCode'] === "400" && $data['result'] === "error"){
            $this->redirect('/Pay/Index/fail');
        }else{
            $this -> assign(array(
                'data',$data
            ));
            $this -> display();
        }
    }

    //获取Code值 通过Code 取出用户编号  用户编号当作查询条件 查询 
    public function index(){
        if(!session('Pay_pwd')){
            $this -> display('login');
            die;
        }
//        if(!session('pay_bianhao')){
//
//            $code = $_GET['code'];
//            session('pay_code',$code);
//            $access_token = $this -> Access_token($this -> corpid, $this -> corpsecret );
//            session('pay_access_token',$access_token);
//            $userinfo = $this -> Getinfo(session('pay_access_token'),$this -> Getuserid());
//            $bianhao = $userinfo['extattr']['attrs'][0]['value'];
//            session('pay_bianhao',$bianhao);
//            //后加
//            session('name',$userinfo['name']);
//
//        }
        $this -> display();
    }

    //获取用户userId
    public function Getuserid(){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".session('pay_access_token')."&code=".session('pay_code');
        $userid = json_decode(file_get_contents($url),true)['UserId'];
        return $userid;
    }

    //获取用户详细信息
    public function Getinfo($access_token,$userid){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=$access_token&userid=$userid";
        $userinfo = json_decode(file_get_contents($url),true);
        return $userinfo;
    }

    //通过部门ID 获取部门名称
    public function Getbumen($id){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=".session('pay_access_token')."&id=$id";
        $bumen = json_decode(file_get_contents($url),true);
        return $bumen;
    }
    
    //成功提示
    public function msg(){
        $this -> display();
    }
    
    //失败提示
    public function fail(){
        $this -> display();
    }
    
    //密码错误
    public function failpwd(){
        $this -> display();
    }
    
    //修改密码成功提示
    public function msgpwd(){
        $this -> display();
    }

    //请求超时报错提示
    public function failget(){
        $this -> display();
    }

    //工资查询登陆
    public function login(){
        $redis = connectRedis();
        $redis -> select(9);
        $uuid = $redis -> get(session('pay_bianhao'));
        $pwd = unserialize($uuid)['pwd1'];

        $pwd1 = I('post.pwd1');

        if(IS_POST){
            if( $pwd === $pwd1){
                session('Pay_pwd',$pwd);
                $this ->redirect('Pay/Index/index');
            }else{
                $this ->redirect('Pay/Index/failpwd');
            }
        }
        $this -> display();
    }

    //设置密码
    public function setpwds(){
        $post =  I('post.');
        $post = serialize($post);
        $redis = connectRedis();
        $redis -> select(9);
        $data = $redis -> set(session('pay_bianhao'),$post);

        if($data){
            $this ->redirect('Pay/Index/msgpwd');
        }else{
            $this ->redirect('Pay/Index/failuppwd');
        }
    }
                      
    //入口文件 OAUTH网页授权入口
    public function setpwd(){
        header('Content-type:text/html;charset=utf-8');
        if(!session('pay_bianhao')){

            $code = $_GET['code'];
            session('pay_code',$code);
            $access_token = $this -> Access_token($this -> corpid, $this -> corpsecret );
            session('pay_access_token',$access_token);
            $userinfo = $this -> Getinfo(session('pay_access_token'),$this -> Getuserid());
            $bianhao = $userinfo['extattr']['attrs'][0]['value'];
            session('pay_bianhao',$bianhao);
            //后加
            session('name',$userinfo['name']);
        }

        $redis = connectRedis();
        $redis -> select(9);
        $content = $redis -> get(session('pay_bianhao'));
        //dump($content);exit;
        if($content){

            $this -> redirect('Pay/Index/login');
        }else{

            $this -> display();
        }
	}

    //更改密码
    public function updatepwd(){
        if(IS_POST){
            $post = I('post.');
            $post = serialize($post);
            $redis = connectRedis();
             $redis -> select(9);
             $content = $redis -> set(session('pay_bianhao'),$post);
            if($content){
                $this ->redirect('Pay/Index/msgpwd');
            }else{
                $this ->redirect('Pay/Index/failuppwd');
            }
        }
        $this -> display();
    }
        
    //验证密保问题和答案ajax
    public function  verification(){
        if(IS_POST){
            $select2 = I('post.select2');
            $pwd4 = I('post.pwd4');
            $redis = connectRedis();
            $redis -> select(9);
            $content = $redis -> get(session('pay_bianhao'));
            $data = unserialize($content);    
            $rslect2 = $data['select2'];
            $rpwd4 =  $data['pwd4'];
            if($select2 !== $rslect2 ){
                echo 1;die;
            }else if( $pwd4 !== $rpwd4){
                echo 2;die;
            }
        }
     }

     public function rdis(){
        $arr = array(
            '2016-01' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-02' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '400',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8825'
            ),
            '2016-03' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '800',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8725'
            ),
            '2016-04' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1200',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '9125'
            ),
            '2016-05' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-06' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-07' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-08' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-09' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-10' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-11' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2016-12' => array(
                '基本工资' => '5800',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2017-01' => array(
                '基本工资' => '6000',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1000',
                '扣除保险' => '675',
                '违规违纪' => '500',
                '其他扣款' => '',
                '实发工资' => '8825'
            ),
            '2017-02' => array(
                '基本工资' => '6000',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '500',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8625'
            ),
            '2017-03' => array(
                '基本工资' => '6000',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '800',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '8925'
            ),
            '2017-04' => array(
                '基本工资' => '6000',
                '岗位津贴' => '2000',
                '加班工资' => '500',
                '伙食补贴' => '300',
                '交通补贴' => '200',
                '绩效奖金' => '1200',
                '扣除保险' => '675',
                '违规违纪' => '200',
                '其他扣款' => '',
                '实发工资' => '9325'
            )
        );
        file_put_contents('./Org/Chat/test.txt',serialize($arr));
     }
}

